"""自定义异常类"""

class BaseException(Exception):
    """基础异常类"""
    
    def __init__(self, message: str, code: int = None, details: dict = None):
        self.message = message
        self.code = code
        self.details = details or {}
        super().__init__(self.message)
    
    def to_dict(self) -> dict:
        """转换为字典格式"""
        return {
            'message': self.message,
            'code': self.code,
            'details': self.details
        }

class ValidationException(BaseException):
    """数据验证异常"""
    
    def __init__(self, message: str, field: str = None, details: dict = None):
        super().__init__(message, code=400, details=details)
        self.field = field
    
    def to_dict(self) -> dict:
        result = super().to_dict()
        if self.field:
            result['field'] = self.field
        return result

class BusinessException(BaseException):
    """业务逻辑异常"""
    
    def __init__(self, message: str, code: int = 422, details: dict = None):
        super().__init__(message, code=code, details=details)

class AuthenticationException(BaseException):
    """认证异常"""
    
    def __init__(self, message: str = "认证失败", details: dict = None):
        super().__init__(message, code=401, details=details)

class AuthorizationException(BaseException):
    """授权异常"""
    
    def __init__(self, message: str = "权限不足", details: dict = None):
        super().__init__(message, code=403, details=details)

class NotFoundException(BaseException):
    """资源不存在异常"""
    
    def __init__(self, message: str = "资源不存在", resource_type: str = None, resource_id: str = None):
        super().__init__(message, code=404)
        self.resource_type = resource_type
        self.resource_id = resource_id
    
    def to_dict(self) -> dict:
        result = super().to_dict()
        if self.resource_type:
            result['resource_type'] = self.resource_type
        if self.resource_id:
            result['resource_id'] = self.resource_id
        return result

class ConflictException(BaseException):
    """资源冲突异常"""
    
    def __init__(self, message: str = "资源冲突", details: dict = None):
        super().__init__(message, code=409, details=details)

class RateLimitException(BaseException):
    """限流异常"""
    
    def __init__(self, message: str = "请求过于频繁", retry_after: int = None):
        super().__init__(message, code=429)
        self.retry_after = retry_after
    
    def to_dict(self) -> dict:
        result = super().to_dict()
        if self.retry_after:
            result['retry_after'] = self.retry_after
        return result

class DatabaseException(BaseException):
    """数据库异常"""
    
    def __init__(self, message: str = "数据库操作失败", details: dict = None):
        super().__init__(message, code=500, details=details)

class ExternalServiceException(BaseException):
    """外部服务异常"""
    
    def __init__(self, message: str = "外部服务调用失败", service_name: str = None, details: dict = None):
        super().__init__(message, code=502, details=details)
        self.service_name = service_name
    
    def to_dict(self) -> dict:
        result = super().to_dict()
        if self.service_name:
            result['service_name'] = self.service_name
        return result